package com.myaihui.controller;

import com.myaihui.config.PermissionCode;
import com.myaihui.domain.Content;
import com.myaihui.domain.SysUser;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.msg.MsgDefinition;
import com.myaihui.foundation.msg.ResultMsg;
import com.myaihui.foundation.query.ContentQuery;
import com.myaihui.service.ContentService;
import com.myaihui.util.LoginUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by sun on 2018/6/4.
 *
 * @author sunfuchang03@126.com
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/content")
public class ContentController {
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Static fields/constants/initializer
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    private static Logger log = LoggerFactory.getLogger(ContentController.class);

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Instance fields
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @Autowired
    private ContentService contentService;

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Constructors
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */



    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Public Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @GetMapping("/")
    @RequiresPermissions(PermissionCode.Content.CONTENT_PAGE_HOME)
    public String home() {
        return "content/home";
    }

    @GetMapping("/add")
    @RequiresPermissions(PermissionCode.Content.CONTENT_PAGE_ADD)
    public String edit(Model model) {
        return "content/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions(PermissionCode.Content.CONTENT_PAGE_EDIT)
    public String edit(@PathVariable String id, Model model) {
        model.addAttribute("content", contentService.get(id));
        return "content/edit";
    }

    @GetMapping("/{id}")
    @ResponseBody
    @RequiresPermissions(PermissionCode.Content.CONTENT_QUERY_GET_BY_ID)
    public ResultMsg get(@PathVariable String id, Model model) {
        if (!StringUtils.hasLength(id)) {
            return ResultMsg.error(MsgDefinition.EMPTY_ARGUMENTS);
        }
        Content content = contentService.get(id);
        model.addAttribute("content", content);
        return ResultMsg.success(content);
    }

    @PostMapping("/page")
    @ResponseBody
    @RequiresPermissions(PermissionCode.Content.CONTENT_QUERY_PAGE_QUERY)
    public ResultMsg queryPageData(ContentQuery query) {
        String subjectId = LoginUtil.loginUser().getPlatformId();
        query.setSubjectId(subjectId);
        Pager<Content> contentPager = contentService.pageQuery(query);
        return ResultMsg.success(contentPager);
    }

    @PostMapping("/save")
    @ResponseBody
    @RequiresPermissions(PermissionCode.Content.CONTENT_UPDATE_SAVE)
    public ResultMsg saveOrUpdate(Content content) {
        String id = content.getId();
        content.setState(Content.State.EDITING);

        SysUser sysUser = LoginUtil.loginUser();
        content.setCreateUserId(sysUser.getId());
        content.setCreateUserPlatform(sysUser.getPlatformId());

        if (StringUtils.hasLength(id)) {
            content = contentService.update(content);
        } else {
            content = contentService.add(content);
        }
        return ResultMsg.success(content);
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    @RequiresPermissions(PermissionCode.Content.CONTENT_DELETE_DELETE_BY_ID)
    public ResultMsg delete(@PathVariable String id) {
        if (!StringUtils.hasLength(id)) {
            return ResultMsg.error(MsgDefinition.EMPTY_ARGUMENTS);
        }
        contentService.delete(id);
        return ResultMsg.success(true);
    }

    @PostMapping("/publish/{id}")
    @ResponseBody
    @RequiresPermissions(PermissionCode.Content.CONTENT_UPDATE_PUBLISH)
    public ResultMsg publish(@PathVariable String id) {
        if (!StringUtils.hasLength(id)) {
            return ResultMsg.error(MsgDefinition.EMPTY_ARGUMENTS);
        }
        Content content = contentService.get(id);
        content.setState(Content.State.PUBLISHED);
        content.setPublished(new Date());
        contentService.update(content);
        return ResultMsg.success(true);
    }

    @PostMapping("/cancelPub/{id}")
    @ResponseBody
    @RequiresPermissions(PermissionCode.Content.CONTENT_UPDATE_CANCEL_PUBLISH)
    public ResultMsg cancelPublish(@PathVariable String id) {
        if (!StringUtils.hasLength(id)) {
            return ResultMsg.error(MsgDefinition.EMPTY_ARGUMENTS);
        }
        Content content = contentService.get(id);
        content.setState(Content.State.EDITING);
        contentService.update(content);
        return ResultMsg.success(true);
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Private Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */


}